import {
    AfterViewInit,
    Component,
    EventEmitter,
    Input,
    OnChanges,
    OnDestroy,
    OnInit,
    Output,
    Renderer2,
    SimpleChanges,
    ViewChild
} from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { ConfigModule } from 'src/app/config.module';
import { ActivatedRoute, Router } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { Store } from '@ngxs/store';
import { DragDrop } from '@angular/cdk/drag-drop';
import { NgxPermissionsService } from 'ngx-permissions';
import { NzTreeNodeOptions } from 'ng-zorro-antd/core/tree/nz-tree-base-node';

@Component({
    selector: 'app-select-tree',
    templateUrl: './select-tree.component.html',
    styleUrls: ['./select-tree.component.scss']
})
export class SelectTreeComponent implements OnInit, AfterViewInit, OnDestroy, OnChanges {

    @ViewChild('modalContent') modalContent;
    @ViewChild('modalFooter') modalFooter;

    @Input()
    tree: NzTreeNodeOptions[] = []; //菜单树的数据

    @Input()
    defaultSelectedKeys: Array<string> = []; //默认选中的key
    @Output()
    defaultSelectedKeysChange = new EventEmitter<Array<string>>();

    modalLoadingState: boolean;
    saveLoadingState: boolean;
    treeSearchValue = ''; //树搜索关键字

    constructor(
        public _config: ConfigModule,
        private _formBuilder: FormBuilder,
        private _router: Router,
        private _nzModalService: NzModalService,
        private _activatedRoute: ActivatedRoute,
        private _renderer2: Renderer2,
        private _dragDrop: DragDrop,
        private _store: Store,
        private _ngxPermissionsService: NgxPermissionsService
    ) {
    }

    ngOnChanges(changes: SimpleChanges): void {

        if (changes.hasOwnProperty('allTree')){
            if (this.tree){
                this.tree = [...this.tree];
                // nzData 值异步获取变化后重新渲染一下属性
                this.defaultSelectedKeys = [...this.defaultSelectedKeys];
            }
        }

    }

    ngOnInit() {
    }

    ngAfterViewInit() {
    }

    ngOnDestroy() {
    }

    /**
     * 选中
     * @param event
     */
    /**
     * 点击树菜单的事件
     * @param ev 点击树的菜单传递过来的事件
     */
    nzSelect(ev: any) {

        this.defaultSelectedKeys = ev.keys;

        if (ev.node.isSelected === false) {
            this.defaultSelectedKeys = ['0'];
        }

        this.defaultSelectedKeysChange.emit(this.defaultSelectedKeys);

    }
}
